package pl.mchaniewski.ryneczek.utilities;

import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class NavigationUtil {
    private static final Logger logger = LoggerFactory
            .getLogger(NavigationUtil.class);
    public static final String SAME_PAGE = null;
    public static final String INDEX = "index";
    public static final String REGISTER_SUCCESS = "success";
    public static final String USER_LIST = "userList";
    public static final String DIRECT_INDEX = "/home";
    public static final String DIRECT_USER_LIST = "/admin/userlist";
    public static final String DIRECT_QUOTATION_LIST = "/user/portfolio/quotations";
    public static final String DIRECT_COMPANY = "/user/portfolio/company/";

    public static String facesRedirectTo(String to) {
        return to + "?faces-redirect=true";
    }

    public static void directRedirect(String url) {
        try {
            FacesContext.getCurrentInstance().getExternalContext()
                    .redirect(getContextPath() + url);
        } catch (Exception ex) {
            logger.warn("Couldn't redirect to {} Exception: {}", url, ex);
        }
    }

    private static String getContextPath() {
        Object request = FacesContext.getCurrentInstance().getExternalContext()
                .getRequest();
        String contextPath = null;
        if (request instanceof HttpServletRequest) {
            contextPath = ((HttpServletRequest) request).getContextPath();
        }

        return contextPath;
    }

}
